<?php
/**
 * RedCross Application
 *
 * @copyright Copyright (c) 2010, Kristian Stokke Tryggestad, Rune Vikestad, Andreas Flaten Wist, Simen Nicolai Echholt, Ole Henrik Jahren
 */


require_once(realpath(dirname(__FILE__)) . '/../../tcpdf/config/lang/eng.php');
require_once(realpath(dirname(__FILE__)) . '/../../tcpdf/tcpdf.php');

/**
 * Base class for generating pdf within the RedCross application.
 *
 * @category RedCross
 * @package RedCross_Pdf
 */

class RedCross_Pdf_Base extends TCPDF
{
	/**
	 * Constructor
	 *
	 * @param string $title Title for the PDF document
	 */
	public function __construct($title = 'Unnamed pdf')
	{
		global $l;

		parent::__construct('P', 'mm', 'A4', true, 'UTF-8', false);
		$this->setCreator(PDF_CREATOR);
		$this->setTitle($title);
		//$this->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
		$this->SetMargins(7, PDF_MARGIN_TOP, 7);
		$this->SetHeaderMargin(PDF_MARGIN_HEADER);
		$this->SetFooterMargin(PDF_MARGIN_FOOTER);
		
		$this->SetAutoPageBreak(true, PDF_MARGIN_BOTTOM);
		$this->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
		$this->setImageScale(PDF_IMAGE_SCALE_RATIO);
		/* See config/lang/ for this value */
		$this->setLanguageArray($l);
		$this->SetFont('helvetica', '', 12);
		$this->SetDrawColor(64, 64, 64);
		$this->SetLineWidth(0.3);
		$this->SetFillColor(244, 244, 244);
		$this->SetTextColor(0);
		$this->AddPage();
	}

	public function ruler($width)
	{
		$x = $this->GetX();
		$y = $this->GetY();
		$prev_width = $this-GetLineWidth();
		$this->SetLineWidth($prev_width * 2);
		$this->Line($x, $y, $x + $width, $y);
		$this->SetLineWidth($prev_width);
	}

	/**
	 * Draws a row from an array og cells. The drawing is done in two
	 * passes, so if any of the text is too wide to fit in the cell,
	 * if will be broken into multiple rows automatically and the border
	 * adjusted automatically.
	 *
	 * @param array @cells Array of cells
	 * @return integer Number of rows the row actually took up
	 */
	public function MultiRow(array $cells)
	{
		$maxcc = 1;
		$x = $this->GetX();
		$y = $this->GetY();
		$page = $this->getPage();
		foreach ($cells as $cell)
		{
			$this->setPage($page);
			$font_size = $this->getFontSize();
			$font_style = $this->getFontStyle();
			$this->setFont('', $cell['font_style'], $cell['font_size']);
			$tmp = $this->MultiCell($cell['width'], $cell['height'], $cell['text'], 0, $cell['align'], 0, 0);
			$this->setFont('', $font_style, $font_size);
			if ($tmp > $maxcc)
			{
				$maxcc = $tmp;
			}
		}
		$this->SetXY($x, $y);
		foreach ($cells as $cell)
		{
			$this->setPage($page);
			$this->MultiCell($cell['width'], $cell['height'] * $maxcc, '', $cell['border'], 0, $cell['fill'], 0);
		}
		return $maxcc;
	}

	/**
	 * Helper function for creating a cell.
	 *
	 * @param string $text Text content of the cell
	 * @param integer $width Width of the cell
	 * @param integer $fill If the cell is to be filled or not, see
	 *	TCPDF documentation
	 * @param string $font_style The styling of the font, see TCPDF
	 *	documentation
	 * @param $align Specifies the alignment of the text, see TCPDF
	 *	documentation
	 * @param integer $font_size the size of the font, see TCPDF
	 *	documentation
	 * @param $border Specifies where/if border is drawn around the
	 *	cell, see TCPDF documentation
	 * @return array Containing the cell info
	 */
	public static function make_cell($text, $width, $fill = 0,
		$font_style = '', $align = 'L', $font_size = 12,
		$border = 'LTRB')
	{
		return array('text' => $text, 'width' => $width,
				'height' => intval(ceil($font_size / 2)),
				'border' => $border, 'fill' => $fill,
				'align' => $align, 'font_style' => $font_style,
				'font_size' => $font_size);
	}

	/**
	 * Helper function for creating a row.
	 *
	 * @param Any number of cell arrays
	 * @return array Array of cell arrays
	 */
	public static function make_row()
	{
		return func_get_args();
	}
}
